package com.saturn.titan.core.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.saturn.titan.core.bean.BaseIRBill;
import com.saturn.titan.core.bean.BusiComponentApplyBill;
import com.saturn.titan.core.bean.BusiComponentApplyBillDtl;
import com.saturn.titan.core.bean.BusiDeployBill;
import com.saturn.titan.core.bean.BusiInstallBill;
import com.saturn.titan.core.bean.BusiRepairBill;
import com.saturn.titan.core.bean.BusiWorkBill;
import com.saturn.titan.core.comm.BillType;
import com.saturn.titan.core.comm.Constants;
import com.saturn.titan.core.dto.ConfermWorkBillDto;
import com.saturn.titan.core.dto.FinishBillDto;
import com.saturn.titan.core.dto.RepairBillDto;
import com.saturn.titan.core.repository.mybatis.BusiComponentApplyBillDao;
import com.saturn.titan.core.repository.mybatis.BusiComponentApplyBillDtlDao;
import com.saturn.titan.core.repository.mybatis.BusiDeployBillDao;
import com.saturn.titan.core.repository.mybatis.BusiIntsallBillDao;
import com.saturn.titan.core.repository.mybatis.BusiRepairBillDao;
import com.saturn.titan.core.repository.mybatis.BusiWorkBillDao;

@Component
@Transactional(readOnly = true)
public class RepairAndInstallBillService {

	@Autowired
	private BillNoService billNoService;
	
	@Autowired
	private BusiIntsallBillDao busiIntsallBillDao;
	
	@Autowired
	private BusiRepairBillDao busiRepairBillDao;

	@Autowired
	private BusiComponentApplyBillDao busiComponentApplyBillDao;
	
	@Autowired
	private BusiComponentApplyBillDtlDao busiComponentApplyBillDtlDao;
	
	@Autowired
	private BusiWorkBillDao busiWorkBillDao;
	
	@Autowired
	private BusiDeployBillDao busiDeployBillDao;
	
	
	public BusiRepairBill findRepairBillById(Long id){
		
		BusiRepairBill rb = busiRepairBillDao.selectById(id);
		BusiComponentApplyBill condition = new BusiComponentApplyBill();
		condition.setOrderId(rb.getId());
		condition.setOrgId(rb.getOrgId());
		condition.setType(Constants.REQUEST_TYPE_REPARE);
		
		List<BusiComponentApplyBill> result = busiComponentApplyBillDao.select(condition);
		if(!result.isEmpty()){
			BusiComponentApplyBill tmp = result.get(0);
			
			BusiComponentApplyBillDtl dtlC = new BusiComponentApplyBillDtl();
			dtlC.setBillId(tmp.getId());
			tmp.setDtls(busiComponentApplyBillDtlDao.select(dtlC));
			
			rb.setComponents(tmp);
		}
		
		return busiRepairBillDao.selectById(id);
	}
	
	public BusiInstallBill findInstallBillById(Long id){
		return busiIntsallBillDao.selectById(id);
	}

	public void updateRepairBillById(BusiRepairBill bean){
		busiRepairBillDao.update(bean);
	}
	
	public void updateInstallBillById(BusiInstallBill bean){
		busiIntsallBillDao.update(bean);
	}

	/**
	 * 确认接收安装单
	 * @param dto
	 */
	@Transactional(readOnly = false,rollbackFor=Exception.class)
	public void confirmInstall(ConfermWorkBillDto dto) throws Exception{
		
		BusiWorkBill bill = busiWorkBillDao.selectById(dto.getBillId());
		
		if(bill == null){
			throw new Exception("未找到工单");
		}
		if(!Integer.valueOf(6).equals(bill.getBillStt())){
			throw new Exception("该工单不是待接收状态");
		}
		//更新工单状态为1待处理
		bill.setBillStt(1);
		busiWorkBillDao.update(bill);
		
		//查询对应工单、状态为打开的派工单
		BusiDeployBill condition = new BusiDeployBill();
		condition.setWorkBillId(bill.getId());
		condition.setBillStt(0);
		List<BusiDeployBill> result = busiDeployBillDao.select(condition);
		if(result.isEmpty()){
			throw new Exception("找不到对应派工单");
		}
		if(result.size() > 1){
			throw new Exception("工单存在重复派工情况");
		}
		
		BusiDeployBill currentBusiDeployBill = result.get(0);
		
		//接收后，新增安装单
		BusiInstallBill ib = new BusiInstallBill();
		ib.setOrgId(bill.getOrgId());
		ib.setBillNo(billNoService.getNextBillNo(BillType.IB));
		ib.setWorkBillNo(bill.getBillNo());
		ib.setWorkBillId(bill.getId());
		ib.setDeployBillId(currentBusiDeployBill.getId());
		ib.setEmpId(currentBusiDeployBill.getEmpId());
		ib.setEmergentType(bill.getEmergentType());
		ib.setStartDate(bill.getStartDate());
		ib.setEndDate(bill.getEndDate());
		ib.setConfirmDate(new Date());
		ib.setBillStt(1);
		ib.setRemark(dto.getRemark());
		ib.setCrtOpr(dto.getEmpId());
		ib.setCrtDate(new Date());
		busiIntsallBillDao.insert(ib);
		
		//录入零件申请单
		if(dto.getItms() != null && dto.getItms().size() > 0){
			BusiComponentApplyBill ab = new BusiComponentApplyBill();
			ab.setOrgId(bill.getOrgId());
			ab.setBillNo(billNoService.getNextBillNo(BillType.CAB));
			ab.setType(Constants.REQUEST_TYPE_INSTALL);
			ab.setOrderId(dto.getBillId());
			ab.setBillStt(1);
			
			ab.setCrtOpr(dto.getEmpId());
			ab.setCrtDate(new Date());
			
			busiComponentApplyBillDao.insert(ab);
			for(BusiComponentApplyBillDtl dtl : dto.getItms()){
				if(dtl.getItmId() == null){
					throw new Exception("备件产品不能为空");
				}
				if(dtl.getCount() == null){
					dtl.setCount(1);
				}
				dtl.setBillId(ab.getId());
				busiComponentApplyBillDtlDao.insert(dtl);
			}
		}
	}

	@Transactional(readOnly = false,rollbackFor=Exception.class)
	public void confirmRepair(ConfermWorkBillDto dto)throws Exception {
		
		BusiWorkBill bill = busiWorkBillDao.selectById(dto.getBillId());
		
		if(bill == null){
			throw new Exception("未找到工单");
		}
		if(!Integer.valueOf(6).equals(bill.getBillStt())){
			throw new Exception("该工单不是待接收状态");
		}
		//更新工单状态为1待处理
		bill.setBillStt(1);
		busiWorkBillDao.update(bill);
		
		//查询对应工单、状态为打开的派工单
		BusiDeployBill condition = new BusiDeployBill();
		condition.setWorkBillId(bill.getId());
		condition.setBillStt(0);
		List<BusiDeployBill> result = busiDeployBillDao.select(condition);
		if(result.isEmpty()){
			throw new Exception("找不到对应派工单");
		}
		if(result.size() > 1){
			throw new Exception("工单存在重复派工情况");
		}
		
		BusiDeployBill currentBusiDeployBill = result.get(0);
		
		//接收后，新增安装单
		BusiRepairBill ib = new BusiRepairBill();
		ib.setOrgId(bill.getOrgId());
		ib.setBillNo(billNoService.getNextBillNo(BillType.IB));
		ib.setWorkBillId(bill.getId());
		ib.setDeployBillId(currentBusiDeployBill.getId());
		ib.setEmpId(currentBusiDeployBill.getEmpId());
		ib.setEmergentType(bill.getEmergentType());
		ib.setStartDate(bill.getStartDate());
		ib.setEndDate(bill.getEndDate());
		ib.setConfirmDate(new Date());
		ib.setBillStt(1);
		ib.setRemark(dto.getRemark());
		ib.setCrtOpr(dto.getEmpId());
		ib.setCrtDate(new Date());
		busiRepairBillDao.insert(ib);
		
		//录入零件申请单
		if(dto.getItms() != null && dto.getItms().size() > 0){
			BusiComponentApplyBill ab = new BusiComponentApplyBill();
			ab.setOrgId(bill.getOrgId());
			ab.setBillNo(billNoService.getNextBillNo(BillType.CAB));
			ab.setType(Constants.REQUEST_TYPE_REPARE);
			ab.setOrderId(dto.getBillId());
			ab.setBillStt(1);
			
			ab.setCrtOpr(dto.getEmpId());
			ab.setCrtDate(new Date());
			
			busiComponentApplyBillDao.insert(ab);
			for(BusiComponentApplyBillDtl dtl : dto.getItms()){
				if(dtl.getItmId() == null){
					throw new Exception("备件产品不能为空");
				}
				if(dtl.getCount() == null){
					dtl.setCount(1);
				}
				dtl.setBillId(ab.getId());
				busiComponentApplyBillDtlDao.insert(dtl);
			}
		}
		
	}
	

	public List<BusiInstallBill> selectInstallBillList(BusiInstallBill bill) {
		return busiIntsallBillDao.selectInstallBillList(bill);
	}

	@Transactional(readOnly = false,rollbackFor=Exception.class)
	public void finishOrder(FinishBillDto dto)throws Exception {
		//更新安装单状态
		BaseIRBill bill = null;
		if(Constants.REQUEST_TYPE_INSTALL.equals(dto.getType())){
			bill = findInstallBillById(dto.getBillId());
		}else{
			bill = findRepairBillById(dto.getBillId());
		}
		
		if(bill == null){
			throw new Exception("未找到维修单");
		}
		
		if(!Integer.valueOf(1).equals(bill.getBillStt()) && !Integer.valueOf(2).equals(bill.getBillStt())){
			throw new Exception("该维修单非接收状态");
		}
		
		bill.setBillStt(3);
		bill.setFinishRemark(dto.getRemark());
		bill.setLstEdtDate(new Date());
		bill.setFinishDate(new Date());
		bill.setLstEdtOpr(dto.getEmpId());
		
		if(Constants.REQUEST_TYPE_INSTALL.equals(dto.getType())){
			busiIntsallBillDao.update((BusiInstallBill)bill);
		}else{
			busiRepairBillDao.update((BusiRepairBill)bill);
		}
		
		//更新工单状态
		BusiWorkBill wb = busiWorkBillDao.selectById(bill.getWorkBillId());
		
		wb.setBillStt(2);
		
		busiWorkBillDao.update(wb);
		
		//更新派工单id
		BusiDeployBill db = busiDeployBillDao.selectById(bill.getDeployBillId());
		db.setBillStt(1);
		busiDeployBillDao.update(db);
	}
	
	public List<RepairBillDto> getBusiRepairBillByCondition(String billNo,Integer workBillId,Integer deployBillId,String billStt,String emergentType,String empName,String startDate_begin,String startDate_end,String endDate_begin,String endDate_end) {
		return busiRepairBillDao.getBusiRepairBillByCondition(billNo,workBillId,deployBillId,billStt,emergentType,empName,startDate_begin,startDate_end,endDate_begin,endDate_end);
	}
}
